﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        private readonly IProductRepository _productRepository;
        private readonly IProductCategoryRepository _productCategoryRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ProductController(IProductRepository productRepository,IUnitOfWork unitOfWork,IProductCategoryRepository productCategoryRepository)
        {
            _productCategoryRepository = productCategoryRepository;
            _productRepository = productRepository;
            _unitOfWork = unitOfWork;
        }
        private IList<ProductModel>  GetAllproductmodel()
        {
            var product = _productRepository.GetAll().AsQueryable();
            IList<ProductModel> productModels =
                (from p in product
                 select new ProductModel
                            {
                                ID = p.ID,
                                Name = p.Name,
                                ProductCategoy = p.ProductCategory.Name,
                            }
                ).ToList();
            return productModels;
        }
        [GridAction]
        public ActionResult TelerikAjax()
        {
            ViewData["productCategories"] = _productCategoryRepository.GetAll();
            return View(new GridModel(GetAllproductmodel()));
        }
        public ActionResult Index()
        {
            ViewData["productCategories"] = _productCategoryRepository.GetAll();
            return View(GetAllproductmodel());
        }
        [HttpPost]
        [GridAction]
        public ActionResult Insert(string name ,int productcategory)
        {
            var product = new Product
            {
                Name = name,
                ProductCategory = _productCategoryRepository.Get(x => x.ID == productcategory)
            };
            if (TryUpdateModel(product, null, null, new[] { "ProductCategory" }))
            {
                _productRepository.Add(product);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetAllproductmodel()));
        }
        //[HttpPost]
        //[GridAction]
        //public ActionResult Insert(FormCollection form,int productcategory)
        //{
        //    var product = new Product
        //                      {
        //                          Name = form["Name"],
        //                          ProductCategory = _productCategoryRepository.Get(x => x.ID == productcategory)
        //                      };
        //       _productRepository.Add(product);
        //       _unitOfWork.Commit();
        //    return View(new GridModel(GetAllproductmodel()));
        //}
        [HttpPost]
        [GridAction]
        public ActionResult Update(int id, int productcategory)
        {
            var product = _productRepository.Get(p => p.ID == id);
            product.ProductCategory = _productCategoryRepository.Get(x => x.ID == productcategory);
            if (TryUpdateModel(product, null, null, new[] { "ProductCategory" }))
            {
                _productRepository.Update(product);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetAllproductmodel()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Delete(int id)
        {
            var product = _productRepository.GetById(id);
            if(TryUpdateModel(product))
            {
                _productRepository.Delete(product);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetAllproductmodel()));
        }
    }
}
